package com.zhuiyun.project.api.article.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.zhuiyun.project.annotation.OptLog;
import com.zhuiyun.project.api.article.entity.Article;
import com.zhuiyun.project.api.article.mapper.ArticleMapper;
import com.zhuiyun.project.api.article.model.ArticleModel;
import com.zhuiyun.project.api.article.service.ArticleService;
import com.zhuiyun.project.common.errorCode.CommonException;
import com.zhuiyun.project.common.response.CommonResult;
import com.zhuiyun.project.utils.constants.CommonConstants;
import com.zhuiyun.project.utils.pageUtil.PageRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName ArticleController
 * @Description TODO 文章管理控制层
 * @Author 杨友朋
 * @Date 2023/11/15 15:24
 **/
@RestController
@RequestMapping("/Article")
@Api(tags = "文章管理控制层")
@Slf4j
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    @Resource
    ArticleMapper articleMapper;

    /**
     * @Author 杨友朋
     * @Description 查询文章
     * @Date 2023/11/16 9:24
     * @Return CommonResult
     * @param articleModel，pageRequest
     */
    @ApiOperation(value = "查询文章信息")
    @OptLog(modular = "文章管理",type = CommonConstants.SELECT,describe = "查询文章")
    @GetMapping("selectArticle")
    public CommonResult selectArticle(ArticleModel articleModel, PageRequest pageRequest){
            return CommonResult.ok(articleService.selectArticle(articleModel,pageRequest));
    }

    /**
     * @Author 杨友朋
     * @Description 单查查询文章
     * @Date 2023/11/16 9:24
     * @Return CommonResult
     * @param id
     */
    @ApiOperation(value = "id单查询文章信息")
    @OptLog(modular = "文章管理",type = CommonConstants.SELECT,describe = "id单查询文章")
    @GetMapping("selectArticleById")
    public CommonResult selectArticleById(Integer id){
        return articleService.selectArticleById(id);
    }

    /**
    * @Author 杨友朋
    * @Description 添加文章
    * @Date 2023/11/17 13:55
    * @Return CommonResult
    * @param file,articleModel
    */
    @ApiOperation(value = "添加文章")
    @OptLog(modular = "文章管理",type = CommonConstants.ADD,describe = "添加文章")
    @PostMapping("/addArticle")
    public CommonResult addArticle(MultipartFile file,ArticleModel articleModel) throws CommonException {
        return articleService.addArticle(file,articleModel);
    }

    /**
    * @Author 杨友朋
    * @Description 修改文章
    * @Date 2023/11/17 16:47
    * @Return CommonResult
    * @param file,articleModel
    */
    @ApiOperation(value = "修改文章")
    @OptLog(modular = "文章管理",type = CommonConstants.ADD,describe = "修改文章")
    @PutMapping("/updateArticle")
    public CommonResult updateArticle(ArticleModel articleModel, MultipartFile file) throws CommonException {
        return articleService.updateArticle(file,articleModel);
    }

    /**
     * @Author 杨友朋
     * @Description 删除文章
     * @Date 2023/11/16 9:36
     * @Return CommonResult
     * @param articleIds
     */
    @ApiOperation(value = "删除文章")
    @OptLog(modular = "文章管理",type = CommonConstants.DELETE,describe = "删除文章")
    @DeleteMapping("/deleteArticle/{articleIds}")
    public CommonResult deleteArticle(@PathVariable Integer articleIds){
        return articleService.deleteArticle(articleIds);
    }

    /**
     * @Author 杨友朋
     * @Description 修改文章状态
     * @Date 2023/11/16 9:36
     * @Return CommonResult
     * @param articleModel
     */
    @ApiOperation(value = "修改文章状态")
    @OptLog(modular = "文章管理",type = CommonConstants.UPDATE,describe = "修改文章状态")
    @PutMapping("/changeArticleStatus")
    public CommonResult changeArticleStatus(@RequestBody ArticleModel articleModel){
        return articleService.changeArticleStatus(articleModel);
    }

    /**
    * @Author 杨友朋
    * @Description 二维码生成
    * @Date 2023/11/29 11:30
    * @Return byte[]
    * @param content
    */
    @PostMapping(value = "/generateQRCode", produces = MediaType.IMAGE_PNG_VALUE)
    public byte[] generateQRCode(@RequestBody String content) throws Exception {
        int width = 300;
        int height = 300;
        String format = "png";

        Map<EncodeHintType, Object> hints = new HashMap<>();
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");

        QRCodeWriter writer = new QRCodeWriter();
        BitMatrix bitMatrix = writer.encode(content, BarcodeFormat.QR_CODE, width, height, hints);

        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
            }
        }

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ImageIO.write(image, format, outputStream);

        return outputStream.toByteArray();
    }

    /*
    * @Author xy
    * @Description todo 文章管理查询
    * @Date 2023/11/28 11:13
    * @Return
    * @param
    */
    @ApiOperation(value = "文章管理查询")
    @OptLog(modular = "文章管理",type = CommonConstants.SELECT,describe = "文章管理查询")
    @GetMapping("/selectArticleList")
    public CommonResult selectArticleList(){
        QueryWrapper<Article> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("article_status",0);
        queryWrapper.eq("del_flag",0);
        List<Article> articles = articleMapper.selectList(queryWrapper);
        return CommonResult.ok(articles);
    }
}
